<template>
    <div>
        <Modal v-model="isShowModal" :title="title" @on-cancel="hide('formValidate')" :mask-closable="false">


            <Form ref="formValidate" :model="params" :rules="ruleValidate" :label-width="110">
                <FormItem label="菜单级别：" prop="level">
                    <Select v-model="params.level" placeholder="菜单级别：" clearable>
                        <Option value="1">一级菜单</Option>
                        <Option value="2">二级菜单</Option>
                    </Select>
                </FormItem>
                <FormItem label="上级菜单ID：" prop="parentId" v-if="params.level == 2">
                    <Select v-model="params.parentId" :model-value="params.parentId"  placeholder="请选择上级菜单：" clearable>
                        <Option v-for="(item,index) in parentList" :value="item.adminPermissionId" :key="index">
                            {{item.title}}
                        </Option>
                    </Select>
                   
                </FormItem>
                <FormItem label="菜单ID：" prop="adminPermissionId">
                    <Input maxlength="128" v-model.trim="params.adminPermissionId" placeholder="请输入菜单ID"></Input>
                </FormItem>
                <FormItem label="菜单名称：" prop="permissionName">
                    <Input maxlength="128" v-model.trim="params.permissionName" placeholder="请输入菜单名称"></Input>
                </FormItem>
                <FormItem label="URL：" prop="permissionUrl">
                    <Input maxlength="128" v-model.trim="params.permissionUrl" placeholder="请输入菜单路径"></Input>
                </FormItem>
                <FormItem label="排序：" prop="permissionIndex">
                    <Input maxlength="128" v-model.trim="params.permissionIndex" placeholder="请输入菜单排序"></Input>
                </FormItem>
            </Form>


            <!-- 重写对话框底部按钮 -->
            <div slot="footer">
                <Button @click="hide('formValidate')">取消</Button>
                <Button v-if="type != 1" type="primary" @click="handleSubmit('formValidate')" :loading="loadingBtn">
                    <span v-if="!loadingBtn">确定</span>
                    <span v-else>请稍候...</span>
                </Button>
            </div>

        </Modal>
    </div>
</template>

<script>
    export default {
        name: "role-ope",
        data() {
            return {
                isShowModal: false,
                loadingBtn: false,
                title: '查看信息',
                type: 0,
                params: {
                    level: '1',
                    adminPermissionId: '',
                    permissionName: '',
                    permissionUrl: '',
                    parentId: '',
                    permissionIndex: '',
                },
                parentList: [],
                currentUserRoleList: [],
                ruleValidate: {
                    permissionName: [
                        {required: true, message: '菜单名称不能为空', trigger: 'change'},
                        {max: 128, message: '菜单名称过长', trigger: 'change'}
                    ],
                    adminPermissionId: [
                        {required: true, message: '菜单ID不能为空', trigger: 'change'},
                    ],
                    level: [
                        {required: true, message: '请选择菜单级别', trigger: 'change'},
                    ]
                }
            }
        },
        mounted() {
            // 权限-获取所有的权限
            this.permissionList()
        },
        methods: {
            // 点击提交验证
            handleSubmit(name) {
                console.log(this.params);
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        // this.$Message.error('验证通过');
                        /**
                         * type判断
                         * 1.查看 2.新增 3.编辑
                         */
                        switch (this.type) {
                            case 1:
                                // 查看菜单;
                                break;
                            case 2:
                                // 新增菜单
                                this.addRole();
                                break;
                            case 3:
                                // 编辑菜单
                                this.updateRole();
                                break;
                            default:
                                this.$Notice.error({
                                    title: '管理员管理',
                                    desc: '查看，新增，编辑区分判断报错',
                                    duration: 0
                                });
                                break
                        }
                    } else {
                        this.$Message.error('还有信息没有填写合格哦~');
                    }
                })
            },
            // 显示模态框
            show(type, row) {
                this.isShowModal = true;
                this.type = type;
                /**
                 * type判断
                 * 1.查看 2.新增 3.编辑
                 */
                switch (type) {
                    case 1:
                        this.title = '查看菜单';
                        this.findRoleInfo(row);
                        break;
                    case 2:
                        this.title = '新增菜单';
                        break;
                    case 3:
                        this.title = '编辑菜单';
                        this.findRoleInfo(row);
                        break;
                    default:
                        this.$Notice.error({
                            title: '菜单管理',
                            desc: '查看，新增，编辑模态框报错',
                            duration: 0
                        });
                        break
                }
            },
            // 添加菜单
            addRole(){
                this.$Spin.show();
                this.axios.post('/adminPermission/add', this.params).then(res => {
                    console.log(res);
                    setTimeout(()=>{
                        this.$Spin.hide();
                    },300);
                    if (res.status == 200) {
                        this.$emit('changeDate');
                        this.$Message.success('操作成功.');
                        this.hide('formValidate');
                    }
                    if(res.status == 201){
                        this.$Message.error(res.msg);
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '新增菜单接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 编辑菜单
            updateRole(){
                this.$Spin.show();
                this.axios.post('/adminPermission/updateRole', this.params).then(res => {
                    console.log(res);
                    setTimeout(()=>{
                        this.$Spin.hide();
                    },300);
                    if (res.status == 200) {
                        this.$emit('changeDate');
                        this.$Message.success('操作成功.');
                        this.hide('formValidate');
                    }
                    if(res.status == 201){
                        this.$Message.error(res.msg);
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '编辑菜单接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 查询管理员详情
            findRoleInfo(row){
                this.$Spin.show();
                this.axios.post('/adminPermission/findInfo', {
                    adminPermissionId: row.adminPermissionId
                }).then(res => {
                    console.log(res);
                    setTimeout(()=>{
                        this.$Spin.hide();
                    },300);
                    if (res.status == 200) {
                        this.params = res.data;
                        this.params.level = res.data.level.toString();
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '查看菜单详情接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 权限-获取所有的权限
            permissionList() {
                this.axios.post('/adminLogin/permissionList', {level: 1}).then(res => {
                    console.log('--------------', res);
                    if (res.status == 200) {
                        this.parentList = res.data;
                        console.log('=========', this.parentList);
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '获取所有权限接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 关闭模态框
            hide(name) {
                this.isShowModal = false;
                this.params.adminRoleId = '';
                // 初始化树节点所有权限，去掉选中的权限
                this.permissionList();
                this.$refs[name].resetFields();
                // console.log(this.params)
            }
        }
    }
</script>

<style scoped>

</style>